package com.fsxgt.datagrid.flow.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.core.utils.ReturnT;
import com.fsxgt.datagrid.flow.bo.FlowInfoBO;
import com.fsxgt.datagrid.flow.bo.FlowMenuBO;
import com.fsxgt.datagrid.flow.service.impl.FlowMenuService;
import com.fsxgt.datagrid.sys.bo.LoginBO;
import com.fsxgt.datagrid.sys.bo.SysUserBO;
import com.fsxgt.datagrid.sys.service.impl.SysUserService;
import com.fsxgt.datagrid.sys.utils.LoginHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "流程菜单接口", value = "FlowMenuController")
@RequestMapping(produces = "application/json; charset=UTF-8", path = "/flowMenu")
@RestController
public class FlowMenuController extends BaseController {

	@Autowired
	private FlowMenuService flowMenuService;

	@Autowired
	private SysUserService sysUserService;

	@ApiOperation(hidden = true, value = "查询目录", notes = "")
	@RequestMapping(path = "/getFlowMenu", method = RequestMethod.POST)
	public ReturnT getFlowMenu(@RequestBody JSONObject param, HttpServletRequest request) {

		Map result = new HashMap();
		result.put("code", "200");


		List list = flowMenuService.getMenuList();

		return new ReturnT(list);

	}

	@ApiOperation(hidden = true, value = "新增目录", notes = "")
	@RequestMapping(path = "/addFlowMenu", method = RequestMethod.POST)
	public ReturnT addFlowMenu(@RequestBody FlowMenuBO flowMenuBO, HttpServletRequest request)
			throws Exception {
		flowMenuService.addFlowMenu(flowMenuBO);
		return new ReturnT();
	}

	@ApiOperation(hidden = true, value = "新增流程", notes = "")
	@RequestMapping(path = "/addFlowInfo", method = RequestMethod.POST)
	public ReturnT addFlowInfo(@RequestBody FlowInfoBO flowInfoBO, HttpServletRequest request)
			throws Exception {
		

		
//		flowInfoBO.setCreateUser(LoginHelper.getCurUser().getUsername());

		flowMenuService.addFlowInfo(flowInfoBO);

		return new ReturnT();

	}

	@ApiOperation(hidden = true, value = "删除流程", notes = "")
	@RequestMapping(path = "/deleteFlowInfo", method = RequestMethod.POST)
	public ReturnT deleteFlowInfo(@RequestBody FlowMenuBO flowMenuBO, HttpServletRequest request)
			throws Exception {

		flowMenuService.deleteFlowObject(flowMenuBO);

		return new ReturnT();

	}

	@ApiOperation(hidden = true, value = "新建主题", notes = "")
	@RequestMapping(path = "/addSubject", method = RequestMethod.POST)
	public ReturnT addSubject(@RequestBody FlowMenuBO flowMenuBO, HttpServletRequest request) throws DataGridException {


		flowMenuBO = flowMenuService.addRootMenu(flowMenuBO);
		
		SysUserBO sysUserBO = LoginHelper.getCurUser();

		sysUserBO.setDefaultSubject(String.valueOf(flowMenuBO.getId()));


		sysUserService.modifySysUser(sysUserBO);
		

		return new ReturnT();
	}

	@ApiOperation(hidden = true, value = "修改流程", notes = "")
	@RequestMapping(path = "/updateFlowInfo", method = RequestMethod.POST)
	public ReturnT updateFlowInfo(@RequestBody FlowMenuBO flowMenuBO, HttpServletRequest request)
			throws Exception {

		flowMenuService.updateFlowObject(flowMenuBO);

		return new ReturnT();

	}

	@ApiOperation(hidden = true, value = "切换主题", notes = "")
	@RequestMapping(path = "/switchSubject", method = RequestMethod.POST)
	public ReturnT switchSubject(@RequestBody FlowMenuBO flowMenuBO, HttpServletRequest request)
			throws DataGridException {
		
		
		SysUserBO sysUserBO = LoginHelper.getCurUser();

		sysUserBO.setDefaultSubject(String.valueOf(flowMenuBO.getId()));

		sysUserService.modifySysUser(sysUserBO);

		return new ReturnT();

	}

}